window.workspace = function(){
	var response  = <?#$respons?>;
	var store, grid, sm, win, form, wsets, wstore, sms, wsm, swin, fsform;
	var sid = {'email':Ext.id(), 'name':Ext.id(), 'sdate':Ext.id(), 'edate':Ext.id(),
		'status':Ext.id(), 'user':Ext.id(), 'visok':Ext.id()};
	function openText(data){
		win && win.close();
		win = new Ext.Window({
			title	: '日志详情',
			iconCls	: 'icon-edit',	
			width	: 760,
			items   : [{html:data, id:sid.preview}],
			closeAction : 'hide'
		});
		win.show();
	};
	sm = new Ext.grid.CheckboxSelectionModel();
	function addSForm(data) {
		if(!swin) {
			fsform = new Ext.form.FormPanel({
				frame		: true,
				border		: false,
				bodyStyle	: 'padding:8px;',
		        labelWidth	: 100,
		        labelAlign  : 'right',
		    	items		: [{
						xtype		: 'hidden',
						name		: 'id',
						value		: 0
					},{   
						fieldLabel	: 'Email',
						name		: 'from',
						vtype		: 'email',
						xtype		: 'textfield',
						allowBlank	: false,
						maxLength	: 127,
						width		: 240
					},{
						xtype		: 'textfield',
						fieldLabel	: 'UserName',
						width		: 180,
						name		: 'user',
						maxLength	: 63,
						allowBlank	: false
					},{   
						fieldLabel	: 'Sender',
						name		: 'fromname',
						xtype		: 'textfield',
						allowBlank	: false,
						maxLength	: 63,
						width		: 180
					},{   
						fieldLabel	: 'Loginpw',
						name		: 'loginpw',
						xtype		: 'textfield',
						maxLength	: 64,
						allowBlank	: false,
						width		: 180
					},{   
						fieldLabel	: 'SMTP-Host',
						name		: 'stmp',
						xtype		: 'textfield',
						allowBlank	: false,
						maxLength	: 63,
						width		: 180
					},{
						xtype		: 'numberfield',
						fieldLabel	: 'SMTP-Port',
						allowBlank	: false,
						width		: 120,
						name		: 'port'
					},{
			    		xtype		: 'combo',
			            store		: new Ext.data.ArrayStore({
				        	fields: ['id','name'],
				        	data: main.cv2Array(response.yes2no)
				    	}),
						displayField	: 'name',
						valueField		: 'id',
						mode			: 'local',
						emptyText		: 'selected...',
			            fieldLabel		: 'IsAuth',
			            width			: 120,
			            allowBlank		: false,
						triggerAction	: 'all',
						hiddenName		: 'isauth',
			            name			: 'isauth'
			        },{
			    		xtype		: 'combo',
			            store		: new Ext.data.ArrayStore({
				        	fields: ['id','name'],
				        	data: main.cv2Array(response.visok)
				    	}),
						displayField	: 'name',
						valueField		: 'id',
						mode			: 'local',
						emptyText		: 'selected...',
			            fieldLabel		: 'Status',
			            width			: 120,
			            allowBlank		: false,
						triggerAction	: 'all',
						hiddenName		: 'status',
			            name			: 'status'
				}]
			});
		    swin = new Ext.Window({
				title	: 'Add SMTP',
				iconCls	: 'icon-add',	
				width	: 420,
				items	: [fsform],
				closeAction : 'hide',
				bbar 	: {
					xtype		: 'toolbar',
					buttonAlign	: 'right',
					items		: [{
					text:'Save', 
					iconCls:'icon-save', 
					url      : '/admin/live/log/email/dosave.html',
					handler:function(){
						if(!fsform.getForm().isValid())
		        			return;
						fsform.getForm().submit({
			        		waitMsg 	: 'data submit processing...',
							waitTitle 	: 'Tips',
			        		clientValidation: true,
			        		url		: '/admin/live/log/email/dosave.html',
				        	success	: function(form, action) {
				        		wstore.reload(); 
				        		swin.hide();
							},
							failure	: main.submitFailure
				    	});
					}}]
				}
			});
		}
		var id = Ext.isObject(data)? data.get('id'):0;
		swin.setTitle((id > 0)? 'Edit SMTP':'Add SMTP');
		swin.setIconClass((id > 0)? 'icon-edit':'icon-add');
		swin.show();
		if(id < 1) {
			fsform.getForm().reset();
		} else {
			fsform.getForm().loadRecord(data);
		}
	}
	function mailSets() {
		if(!wsets) {
			sms    = new Ext.grid.CheckboxSelectionModel();
			wstore = new Ext.data.JsonStore({
			    autoDestroy	: true,
			    url			: '/admin/live/log/email/dosmtp.html',
			    remoteSort	: true,
			    sortInfo	: {
			            field		: 'id',
			            direction	: 'DESC'
				},
				baseParams	: {
					start	: 0,
					limit	: main.pageSize
				},
				autoLoad		: true,
			    root			: 'dataset',
			 	totalProperty	: 'total',
			    fields			: ['id', 'user', 'from', 'fromname', 'loginpw', 'stmp', 'port',
			       'isauth', 'status', 'snum', 'stime']
			});
		    wsets = new Ext.Window({
				title	: 'SMTP Configure',
				iconCls	: 'icon-db',	
				width	: 760,
				items	: [{
					xtype		: 'grid',
				    store		: wstore,
				    autoHeight  : true,
				    columns		: [
				        sms,
						{header: 'SmtpID', width:50, sortable:true, dataIndex:'id'},
						{header: 'UserName', width:80, dataIndex:'user'},
						{header: 'Email', width:140, dataIndex:'from'},
						{header: 'SenderNick', width:80, dataIndex:'fromname'},
						{header: 'SMTP-Host', width:100, dataIndex:'stmp'},
						{header: 'SMTP-Port', width:60, dataIndex:'port'},
						{header: 'IsAuth', width:60, sortable:true, dataIndex:'isauth', renderer:function(v){
							return response.yes2no[v];
						}},
						{header: 'Status', width:60, sortable:true, dataIndex:'status', renderer:function(v){
							return response.visok[v];
						}},
						{header: 'LastTime',width:80, dataIndex:'stime'}
					],
				    viewConfig	: {
				        forceFit	 : true,
				        scrollOffset : 0
				    },
				    listeners	: {
				    	'rowdblclick':function(obj, index, e){
				    		var data = obj.store.getAt(index).copy();
				    		addSForm(data);
				    	}
			    	},
				    sm			: sms,
				    stripeRows	: true,
				    tbar		: [{
				    				text	: 'Reload',
				    				iconCls	: 'icon-refresh',
				    				handler : function(){
				    					wstore.reload(); 
				    					}
				    				},'-',{
				    					text	: 'Add SMTP',
				    					iconCls	: 'icon-add',
				    					
				    					handler : function(){
				    						addSForm(0);
				    					}
				    				},'-',{
				    					text	: 'Delete SMTP',
				    					iconCls	: 'icon-delete',
				    					url		: '/admin/live/log/email/dodelete.html',
				    					handler : function(){
				    						var rcs = sms.getSelections();
				    						if(Ext.isEmpty(rcs)) {
				    							Ext.Msg.alert('Tips', 'plz select delete records.');
				    							return;
				    						}
				    						var ids= [];
				    						Ext.each(rcs, function(item){
				    							ids.push(item.get('id'));
				    						});
				    						Ext.Msg.confirm('Tips','do you want to delete select records?',function(btn){
				    							if(btn=="yes") {
				    								Ext.Ajax.request({
				    									url		: '/admin/live/log/email/dodelete.html',
				    									params	: {'ids':ids.join(',')},
				    									success : function(resp, req){
				    										var data = Ext.decode(resp.responseText);
				    										if(data.code == 0) {
				    											wstore.reload(); 
				    										} else {
				    											Ext.Msg.alert('Tips', data.msg);
				    										}
				    									}
				    								});
				    							}
				    						});
				    				}},'-','<b>Search For：</b>','UserName:',{
				    					xtype       : 'textfield',
										id	    	: sid.user,
										width	    : 100
				    				},'Status:',{
				    		    		xtype		: 'combo',
				    		            store		: new Ext.data.ArrayStore({
				    		            	fields: ['id', 'name'],
				    		            	data  :  main.cv2Array(response.visok)
				    		            }),
				    					displayField	: 'name',
				    					valueField		: 'id',
				    					emptyText		: 'selected...',
				    		            width			: 70,
				    					triggerAction	: 'all',
				    					id      		: sid.visok,
				    					mode			: 'local'
				    		        },{
									text			: 'Do Search',
									iconCls			: 'icon-search',
									handler: function() {
										wstore.baseParams.user   = Ext.getCmp(sid.user).getValue();
										wstore.baseParams.status = Ext.getCmp(sid.visok).getValue();
										wstore.reload(); 
									}
								}],
					bbar		: new Ext.PagingToolbar({
						pageSize	: main.pageSize,
						store		: wstore,
						displayInfo	: true
					})
				}],
				closeAction : 'hide'
			});
		}
		wsets.show();
	}
	
	function addForm(node) {
		if(!win) {
		    form = new Ext.form.FormPanel({
				frame		: true,
				border		: false,
				bodyStyle	: 'padding:8px;',
		        labelWidth	: 100,
		        labelAlign  : 'right',
		    	items		: [{
					xtype		: 'hidden',
					name		: 'id',
					value		: 0
				},{
					xtype		: 'textfield',
					fieldLabel	: 'Title',
					width		: 560,
					name		: 'title',
					maxLength	: 63,
					allowBlank	: false
				},{   
					fieldLabel	: 'Receiver Email',
					name		: 'receiver',
					vtype		: 'email',
					xtype		: 'textfield',
					allowBlank	: false,
					width		: 248
				},{
					xtype		: 'uHtmleditor',
					fieldLabel	: 'Content',
					width		: 560,
					height		: 360,
					uploadUrl	: '/admin/editor.html',
					allowBlank	: false,
					name		: 'msg'
				},{
					xtype		: 'datetimefield',
					fieldLabel	: 'SendTime',
					width		: 248,
					name		: 'crotime'
				},{
					xtype		: 'numberfield',
					fieldLabel	: 'Priority',
					width		: 248,
					name		: 'priority'
				}]
			});
			win = new Ext.Window({
				title	: 'Add Email',
				iconCls	: 'icon-add',	
				width	: 760,
				items	: [form],
				closeAction : 'hide',
				bbar 	: {
					xtype		: 'toolbar',
					buttonAlign	: 'right',
					items		: [{
					text:'保存', 
					iconCls:'icon-save', 
					url		: '/admin/live/log/email/dosave.html',
					handler:function(){
						if(!form.getForm().isValid())
		        			return;
		        		form.getForm().submit({
			        		waitMsg 	: 'data submit processing...',
							waitTitle 	: 'Tips',
			        		clientValidation: true,
			        		url		: '/admin/live/log/email/dosave.html',
				        	success	: function(form, action) {
				        		store.reload(); 
				        		win.hide();
							},
							failure	: main.submitFailure
				    	});
					}}]
				}
			});
		}
		var id = Ext.isObject(node)? node.get('id'):0;
		win.setTitle((id > 0)? 'Edit Email':'Add Email');
		win.setIconClass((id > 0)? 'icon-edit':'icon-add');
		win.show();
		if(id < 1) {
			form.getForm().reset();
		} else {
			form.getForm().loadRecord(node);
		}
	}
	store = new Ext.data.JsonStore({
	    autoDestroy	: true,
	    url			: '/admin/live/log/email/dolist.html',
	    remoteSort	: true,
	    sortInfo	: {
	            field		: 'id',
	            direction	: 'DESC'
		},
		baseParams	: {
			start	: 0,
			limit	: main.pageSize
		},
		autoLoad		: true,
	    root			: 'dataset',
	 	totalProperty	: 'total',
	    fields			: ['id', 'title', 'msg', 'receiver', 'status', 'sndtime', 'priority', 'mailid', 'sysid', 'stime']
	});
 	grid = {
		xtype		: 'grid',
		title		: response.title,
	    store		: store,
		region		: 'center', 
	    columns		: [
	        sm,
			{header: 'ID', width:50, sortable:true, dataIndex:'id'},
			{header: 'Title', width:140, dataIndex:'title'},
			{header: 'Receiver Email', width:100, dataIndex:'receiver'},
			{header: 'Status', width:60, sortable:true, dataIndex:'status', renderer:function(v){
				return response.status[v];
			}},
			{header: 'SendTime', width:100,  dataIndex:'sndtime'},
			{header: 'Priority', width:70, sortable:true, dataIndex:'priority'},
			{header: 'SystemID', width:70, dataIndex:'sysid'},
			{header: 'LastTime',width:110, dataIndex:'stime'}
		],
	    viewConfig	: {
	        forceFit	 : true,
	        scrollOffset : 0
	    },
	    listeners	: {
	    	'destroy':function(){
	    		win && win.close();
	    		wsets && wsets.close();
	    	},'rowdblclick':function(grid, index, e){
	    		var data = grid.store.getAt(index).copy();
				openText(data.get('msg'));
	    	}
    	},
	    sm			: sm,
	    stripeRows	: true,
	    tbar		: [{
	    				text	: 'Reload',
	    				iconCls	: 'icon-refresh',
	    				handler : function(){
								store.reload(); 
	    					}
	    				},'-',{
	    					text	: 'ReSend',
	    					iconCls	: 'icon-edit',
	    					url		: '/admin/live/log/email/dosend.html',
	    					handler : function() {
	    						var rcs = grid.sm.getSelections();
	    						if(Ext.isEmpty(rcs)) {
	    							Ext.Msg.alert('Tips', 'plz select send records.');
	    							return;
	    						}
	    						var ids= [];
	    						Ext.each(rcs, function(item){
	    							ids.push(item.get('id'));
	    						});
	    						var mask = new Ext.LoadMask(Ext.getBody(), {msg:"script runing，plz waiting..."});
	    						mask.show();
	    						Ext.Ajax.request({
	    							timeout : 600000,
	    							url		: '/admin/live/log/email/dosend.html',
	    							params	: {'ids':ids.join(',')},
	    							success	: function(resp, req){
	    								mask.hide();
	    				        		var data = Ext.decode(resp.responseText);
	    				        		Ext.Msg.alert('Tips', data.msg);
	    				        		store.reload(); 
	    				        	},
	    							failure	: function(){
	    								mask.hide();
	    								Ext.Msg.alert('Tips', 'system error！');
	    							}
	    						});
	    					}
	    				},'-',{
	    					text	: 'Configure SMTP',
	    					iconCls	: 'icon-db',
	    					handler : function(){
	    						mailSets();
	    					}
	    				},'-','<b>Search For：</b>','Email:',{
	    					xtype       : 'textfield',
							id	    	: sid.email,
							width	    : 130
	    				},'-','Title:',{
	    					xtype       : 'textfield',
							id	    	: sid.name,
							width	    : 130
	    				},'-','Status:',{
	    		    		xtype		: 'combo',
	    		            store		: new Ext.data.ArrayStore({
	    		            	fields: ['id', 'name'],
	    		            	data  :  main.cv2Array(response.status)
	    		            }),
	    					displayField	: 'name',
	    					valueField		: 'id',
	    					emptyText		: 'selected...',
	    		            width			: 110,
	    					triggerAction	: 'all',
	    					id      		: sid.status,
	    					mode			: 'local'
	    		        },'-','LastTime:',{
							xtype       : 'datefield',
							id  		: sid.sdate,
							width	    : 120
						},'~',{
							xtype       : 'datefield',
							id  		: sid.edate,
							width	    : 120
						},'-',{
						text			: 'Do Search',
						iconCls			: 'icon-search',
						handler: function() {
							store.baseParams.name   = Ext.getCmp(sid.name).getValue();
							store.baseParams.email  = Ext.getCmp(sid.email).getValue();
							store.baseParams.status = Ext.getCmp(sid.status).getValue();
							store.baseParams.edate  = Ext.getCmp(sid.edate).getValue();
							store.baseParams.sdate  = Ext.getCmp(sid.sdate).getValue();
							store.reload(); 
						}
					}],
		bbar		: new Ext.PagingToolbar({
			pageSize	: main.pageSize,
			store		: store,
			displayInfo	: true
		})
	};
	return grid;
}();